package com.jwgf.smarowindingmachine_unity.ui;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;

import androidx.fragment.app.DialogFragment;

import com.jwgf.objectbox.ObjectBox;
import com.jwgf.objectbox.UserLogin;
import com.jwgf.objectbox.UserLogin_;
import com.jwgf.smarowindingmachine_unity.R;

import io.objectbox.Box;

public class UserAuthorityDialog extends DialogFragment {

	private AlertDialog dlg;
	private Runnable callback;

	private EditText userId;
	private EditText password;
	private Spinner writeLevel;
	private Spinner readLevel;

	private Bundle bd;

	public void setCallBack(Runnable callback) {
		this.callback = callback;
	}

	@Override
	public Dialog onCreateDialog(Bundle savedInstanceState) {
		AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
		LayoutInflater inflater = getActivity().getLayoutInflater();
		View v = inflater.inflate(R.layout.dialog_authority, null);
		bd = getArguments();

		userId = (EditText) v.findViewById(R.id.user_id_value);
		password = (EditText) v.findViewById(R.id.password_value);
		writeLevel = (Spinner) v.findViewById(R.id.write_level_value);
		readLevel = (Spinner) v.findViewById(R.id.read_level_value);

		if (bd != null) {
			userId.setText(bd.getString(UserLogin.COL_USER));
			password.setText(bd.getString(UserLogin.COL_PASSWORD));
			writeLevel.setSelection(Integer.valueOf(bd.getString(UserLogin.COL_WRITE_LEVEL)) - 1);
			readLevel.setSelection(Integer.valueOf(bd.getString(UserLogin.COL_READ_LEVEL)) - 1);
		}

		builder.setTitle(R.string.set_authority);
		builder.setView(v)
				.setPositiveButton(R.string.save, new DialogInterface.OnClickListener() {

					@Override
					public void onClick(DialogInterface dialog, int which) {
						save();
					}

				})
				.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {

					@Override
					public void onClick(DialogInterface dialog, int which) {

					}
				});
		dlg = builder.create();
		return dlg;
	}

	private void save() {
		String user = userId.getText().toString();
		String pwd = password.getText().toString();
		String wl = (String) writeLevel.getSelectedItem();
		String rl = (String) readLevel.getSelectedItem();
		if (TextUtils.isEmpty(user) || pwd.isEmpty()) {
			Toast.makeText(getActivity(),
					"User id or password can not be null!", Toast.LENGTH_LONG)
					.show();
		} else {
			Box<UserLogin> box = ObjectBox.get().boxFor(UserLogin.class);
			UserLogin userLogin = box.query().equal(UserLogin_.user, user).build().findUnique();
			if (userLogin != null) {
				userLogin.password = pwd;
				userLogin.writeLevel = Integer.valueOf(wl);
				userLogin.readLevel = Integer.valueOf(rl);
				box.put(userLogin);
			} else {
				userLogin  = new UserLogin();
				userLogin.user = user;
				userLogin.password = pwd;
				userLogin.writeLevel = Integer.valueOf(wl);
				userLogin.readLevel = Integer.valueOf(rl);
				box.put(userLogin);
			}
			if (callback != null) {
				callback.run();
			}
		}
	}

}
